/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package is.bean.entity.preference;

import is.bean.entity.BeanRoutinesStringName;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.ejb.CreateException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import nullutil.NullUtil;

/**
 *
 * @author aspr0
 */
public class PreferenceBean extends BeanRoutinesStringName implements EntityBean
{
    private Preference data = new Preference();

    @Override
    public void setEntityContext(EntityContext aContext)
    {
        super.setEntityContext(aContext); //To change body of generated methods, choose Tools | Templates.
        tableName = "preference";
    }

    @Override
    protected void loadData() throws SQLException
    {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try
        {
            connection = dataSource.getConnection();
            String sql = "select * from preference where name = ?";
            statement = connection.prepareStatement(sql);

            name = (String) context.getPrimaryKey(); //todo resolve primary key after ejbCreate unset

            statement.setString(1, name);
            resultSet = statement.executeQuery();
            resultSet.next();

            data.setName(resultSet.getString("name"));
            data.setValue(resultSet.getString("value"));
        }
        finally
        {
            if (resultSet != null)
            {
                resultSet.close();
            }

            if (statement != null)
            {
                statement.close();
            }
            if (connection != null)
            {
                connection.close();
            }
        }
    }

    @Override
    protected void storeData() throws SQLException
    {
        if (modified)
        {
            Connection connection = null;
            PreparedStatement statement = null;

            try
            {
                connection = dataSource.getConnection();
                String sql = "update preference set name = ?, value = ? where name = ?";
                statement = connection.prepareStatement(sql);

                statement.setString(1, data.getName());
                NullUtil.setStringNullDecide(statement, 2, data.getValue());
                statement.setString(3, data.getName());

                statement.execute();
            }
            finally
            {
                if (statement != null)
                {
                    statement.close();
                }

                if (connection != null)
                {
                    connection.close();
                }
            }
        }
    }

    public Preference getData()
    {
        return data;
    }

    public void setData(Preference data)
    {
        this.data = data;
        modified = true;
    }

    public String ejbCreate(Preference data) throws CreateException, SQLException
    {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try
        {
            connection = dataSource.getConnection();

            String sql = "insert "
                    + "into preference "
                    + "(name, value) "
                    + "values "
                    + "(?, ?)";
            statement = connection.prepareStatement(sql);

            statement.setString(1, data.getName());
            NullUtil.setStringNullDecide(statement, 2, data.getValue());

            statement.execute();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            CreateException ce = new CreateException();
            ce.initCause(e);
            throw ce;
        }
        finally
        {
            if (statement != null)
            {
                statement.close();
            }

            if (connection != null)
            {
                connection.close();
            }

            if (resultSet != null)
            {
                resultSet.close();
            }
        }

//        id = data.getId();
        this.data = data;
        return data.getName();
    }

    public void ejbPostCreate(Preference data) throws CreateException
    {
    }
}
